var App = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    role_id:wx.getStorageSync('role_id'),
    domain:App.globalData.iconsUrl,
    list:[],
    setList:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取数据列表
    this.getDataList();
  },

  add(e){
    let setList = this.data.setList
    let arr = this.data.setList[e.currentTarget.dataset.id]
    if(arr.length >= 6){
      App.showError('最多只能添加6项')
      return false
    }
    arr.push({real_pay:'',prize:'',show:true,});
    setList[e.currentTarget.dataset.id] = arr
    this.setData({
      setList
    })
  },
  delete(e){
    let setList = this.data.setList
    let arr = this.data.setList[e.currentTarget.dataset.id]
    if(arr.length < 2){
      App.showError('最少保留1项')
      return false
    }
    console.log('e.currentTarget.dataset.index',e.currentTarget.dataset.index)
    arr.splice(e.currentTarget.dataset.index, 1);
    setList[e.currentTarget.dataset.id] = arr
    this.setData({
      setList
    })
  },
  // 文本框触发
  bindinput(e){
    let str = e.currentTarget.dataset.name
    let v_arr = str.split("|");
    let setList = this.data.setList
    setList[v_arr[1]][v_arr[2]][v_arr[0]] = e.detail.value
    console.log(setList)
    this.setData({setList})
   },
  //获取数据列表
  getDataList(){
    let that = this;
    let obj = {b_id:App.get_b_id()}
    App.request('/Manage/Management/rechargeConfigList',obj,function(e){
      wx.stopPullDownRefresh()
        if (e.code == "200") {
          let list = e.result
          let setList = {}
          
          list.forEach(function(value, index) {
              let key = value.store_id
              let arr =[]
              let recharge_config = value.recharge_config
              for(let i=0;i<6;i++){
                if(recharge_config[i] != undefined){
                  arr[i]={
                    real_pay:recharge_config[i].real_pay,
                    prize:recharge_config[i].prize,
                    show:true,
                  }
                }
              }
              setList[key] =arr
          });
          
          that.setData({
            list,
            setList
          })
        } 
    })
  },
  // 提交保存
  submit(e){
      let that =  this
      let obj = {
        store_id:e.currentTarget.dataset.id,
        config:JSON.stringify(that.data.setList[e.currentTarget.dataset.id])
      }
      App.request('/Manage/Management/setRechargeConfig',obj,function(e){
          if (e.code == "200") {
            App.showSuccessToast(e.msg);
          } 
      })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getDataList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})